package com.itheima.controller;

import com.itheima.util.CheckCodeUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码
 */
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //生成验证码
        String code = CheckCodeUtil.generateVerifyCode(4);
        System.out.println("验证码是：" + code);
        //将验证码放在会话域中
        request.getSession().setAttribute("code", code);
        //参数：宽，高，输出流，验证码
        CheckCodeUtil.outputImage(100,50,outputStream,code);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
